<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="测评状态" prop="gradeStatus">
        <el-select v-model="queryParams.gradeStatus" placeholder="请选择测评状态" clearable size="small">
          <el-option label="测评中" value="1" />
          <el-option label="完成测评" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户类别" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择用户类别" clearable size="small">
          <el-option label="普通用户" value="1" />
          <el-option label="推广员" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:grade:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gradeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="测评编号" width="160" align="center" prop="gradeNum" />
      <el-table-column label="测评时间" width="160" align="center" prop="createTime" />
      <el-table-column label="提交时间" width="160" align="center" prop="finishTime" />
      <el-table-column label="测评状态" align="center" prop="gradeStatus">
        <template slot-scope="scope">
          <span>{{scope.row.gradeStatus | filterGradeStatus}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户手机号" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.user">{{scope.row.user.phonenumber}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.user">{{scope.row.user.userName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.user">{{scope.row.user.email}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户类别" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.user">{{scope.row.user.userType | filterUserType}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['system:grade:edit']"
          >查看报告</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="delete-btn"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:grade:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改测评成绩对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="测评编号" prop="gradeNum">
          <el-input v-model="form.gradeNum" placeholder="请输入测评编号" />
        </el-form-item>
        <el-form-item label="测评状态">
          <el-radio-group v-model="form.gradeStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="测评用户" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入测评用户" />
        </el-form-item>
        <el-form-item label="测评报告" prop="gradeReport">
          <el-input v-model="form.gradeReport" placeholder="请输入测评报告" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGrade,
  getGrade,
  delGrade,
  addGrade,
  updateGrade,
  exportGrade
} from "@/api/system/grade";

export default {
  name: "Grade",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      gradeNums: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测评成绩表格数据
      gradeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gradeNum: null,
        gradeStatus: null,
        userType: null,
        userId: null,
        gradeReport: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  filters: {
    filterGradeStatus(value) {
      return {
        1: "测评中",
        2: "完成测评"
      }[value];
    },
    filterUserType(value) {
      return {
        1: "普通用户",
        2: "推广员"
      }[value];
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询测评成绩列表 */
    getList() {
      this.loading = true;
      listGrade(this.queryParams).then(response => {
        this.gradeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gradeNum: null,
        gradeStatus: 0,
        userId: null,
        gradeReport: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.gradeNums = selection.map(item => item.gradeNum);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加测评成绩";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGrade(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测评成绩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGrade(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGrade(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const gradeNums = row.gradeNum || this.gradeNums;
      this.$confirm(
        '是否确认删除测评成绩编号为"' + gradeNums + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delGrade(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有测评成绩数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportGrade(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
